﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Transactions;
using System.Web;
using DukeNUS.FacilitiesPlanning.FPSystem.Data;
using DukeNUS.FacilitiesPlanning.FPSystem.Models;
using System.Xml.Linq;

namespace DukeNUS.FacilitiesPlanning.FPSystem.Data.Defaults
{
    public class DefaultCardReaders : IDefaultData
    {

        public void Create()
        {
            try
            {
                using (DukeNUSDatabase db = new DukeNUSDatabase())
                {
                    if (!db.CardReaders.Any())
                    {
                        var cardReaderGroups = db.CardReaderGroups.ToList();
                        var cardReaderTypes = db.CardReaderTypes.ToList();
                        var restrictedAreas = db.CardReaderDepartmentRestrictedAreas.ToList();

                        using (TransactionScope trans = new TransactionScope())
                        {
                            var path = ConfigurationManager.AppSettings["DefaultCardReaders"];
                            var xml = XElement.Load(HttpContext.Current.Server.MapPath(path)); ;

                            var query = from cardReaders in xml.Elements("cardreader")
                                        select cardReaders;

                            foreach (var cardReader in query)
                            {
                                var cardReaderDescription = (string) cardReader.Element("description");
                                var cardReaderNo = (string) cardReader.Element("cardreaderno");
                                var cardReaderType = (string) cardReader.Element("type");
                                var cardReaderGroup = (string) cardReader.Element("group");
                                var restrictedArea = (string)cardReader.Element("restrictedarea");
                                bool needsApproval = (((string) cardReader.Element("needsapproval")).ToLower() == "1") ||
                                                     (((string) cardReader.Element("needsapproval")).ToLower() == "true");
                                bool isactive = (((string) cardReader.Element("isactive")).ToLower() == "1") ||
                                                (((string) cardReader.Element("isactive")).ToLower() == "true");

                                if (!db.CardReaders.Any(cr => ((cr.Description == cardReaderDescription) && cr.CardReaderNo == cardReaderNo)))
                                {
                                    object cardReaderTypeId = null;

                                    if (cardReaderTypes.Any(t => (t.Description == cardReaderType))) cardReaderTypeId = cardReaderTypes.First(t => (t.Description == cardReaderType)).CRTID;

                                    object cardReaderGroupId = null;

                                    if (cardReaderGroups.Any(g => (g.Description == cardReaderGroup))) cardReaderGroupId = cardReaderGroups.First(g => (g.Description == cardReaderGroup)).CRGID;

                                    object restrictedAreaId = null;

                                    if (restrictedAreas.Any(ra => (ra.Description == restrictedArea))) restrictedAreaId = restrictedAreas.First(ra => (ra.Description == restrictedArea)).CRRADID;

                                    var newCardReader = new CardReader()
                                    {
                                        Description = cardReaderDescription,
                                        CardReaderNo = cardReaderNo,
                                        NeedsApproval = needsApproval,
                                        IsActive = isactive
                                    };

                                    if (cardReaderGroupId != null)  newCardReader.CRGID = Convert.ToInt32(cardReaderGroupId.ToString());
                                    if (cardReaderTypeId != null)  newCardReader.CRTID = Convert.ToInt32(cardReaderTypeId.ToString());
                                    if (restrictedAreaId != null)  newCardReader.CRRADID = Convert.ToInt32(restrictedAreaId.ToString());

                                    db.CardReaders.Add(newCardReader);
                                }
                            }

                            db.SaveChanges(); trans.Complete();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry(GetType().FullName, string.Format("Message: {0}\nStack Trace: {1}", ex.Message, ex.StackTrace), EventLogEntryType.Error);
            }
        }
    }

}